<?php
/**根据积分排行分配奖品*/
$log_file = 'user_rank';
require dirname(__FILE__) . '/base.php';
try {
    ignore_user_abort(true);
    set_time_limit(0);
    $transaction = new \Base\BaseOrmModel();
    //参数条件前置
    //$argc == 2只传递一个参数 表示type  1-周排行 2-总排行
    //$argc == 3传递两个参数 表示参数1表示type 参数2表示week_type 1.第一周 2.第二周 3.第三周 4.第四周
    if (!isset($argv[1]) ||  $argv[1] == '') {
        \Util\LogUtil::log($log_file, '缺少TYPE参数');
        exit;
    }
    if ($argv[1] == 1 && (!isset($argv[2]) || $argv[2] == '')) {
        \Util\LogUtil::log($log_file, '缺少WEEK_TYPE参数');
        exit;
    }
    $transaction->beginTransaction();
    if ($argv[1] == 1) {//周排行奖品录入
        $limit = 150;
        $type = '';
        $filed = '';
        switch ($argv[2]) {
            case 1:
                $type = '第一周';
                $filed = 'one_week_score';
                break;
            case 2:
                $type = '第二周';
                $filed = 'two_week_score';
                break;
            case 3:
                $type = '第三周';
                $filed = 'three_week_score';
                break;
            case 4:
                $type = '第四周';
                $filed = 'four_week_score';
                break;
        }
        /*1.查询t_play_showings  判断本周比赛是否已经全部结束*/
        $playShowingBiz = new \Biz\Play\Showings();
        $playShowingRe = $playShowingBiz->getPlayShowingByWeekOwn($argv[2]);
        if (!empty($playShowingRe)) {
            \Util\LogUtil::log($log_file, $type . "的赛事尚未全部结束，不能开奖");
            exit;
        }
        /*2.根据周次查询t_user_account表中用户身上积分排名信息*/
        if($filed == ''){
            \Util\LogUtil::log($log_file, "周次参数错误，不能开奖");
            exit;
        }
        $userAccountBiz = new \Biz\UserAccount();
        $userAccountList = $userAccountBiz->getUserAccountList($filed,$limit);
        if(empty($userAccountList)){
            \Util\LogUtil::log($log_file, "用户积分不存在，不能开奖");
            exit;
        }
        /*3.周排行奖品（每周排名前150的玩家即可随机获得：官方纪念版足球、定制U型枕、定制刀具套装、充电宝）*/
        $productBiz = new \Biz\Product();
        $temp  = array();
        $productList = $productBiz->getProductListByType(1);//type = 1 表示H5互动小游戏初级奖池商品
        foreach ($productList as $key=>$val){
            for ($i = 1;$i<=$val['residue_num'];$i++){
                $temp[] = $val['id'];
            }
        }
        //用户排名积分奖品数据拼接并插入数据库
        $userRankBiz = new \Biz\UserRank();
        foreach ($userAccountList as $k=>$v){
            /*根据user_id 和type，week_type判断是否已经有奖品记录*/
            $re = $userRankBiz->getUserRankByWhere($v['uid'],1,$argv[2]);
            if($re){//数据表中已有该用户奖品记录，跳出本次循环执行下个操作
                continue;
            }
            $tmp = array();
            $em = array_rand($temp);
            $tmp['user_id'] = $v['uid'];
            $tmp['nickname'] = $v['nickname'];
            $tmp['user_sort'] = $k+1;
            $tmp['user_score'] = $v[$filed];
            $tmp['type'] = 1;
            $tmp['week_type'] = $argv[2];
            $tmp['product_id'] = $temp[$em];
            $re = $userRankBiz->insert($tmp);
            if(!$re){
                \Util\LogUtil::log($log_file, "总排行积分奖品录入失败，数据为:".json_encode($tmp,true));
                continue;
            }else{
                $productBiz->updateById($tmp['product_id']);//用户积分派奖成功，对应派奖商品库存减一
            }
        }
    } elseif ($argv[1] == 2) {//总排行奖品录入
        /*总排名：派奖规则
            1名获得俄罗斯双人7日游套餐，
            2~4名获得苹果X，
            5~15名获得拍立得，
            16~30名获得小米电饭煲，
            31~50名获得YSL小羊皮口红，
            51~150名获得运动手环，
            150~180名获得官方纪念版足球。
        */
        /*1.查询t_play_showings  判断所有比赛是否已经全部结束*/
        $playShowingBiz = new \Biz\Play\Showings();
        $playShowingRe = $playShowingBiz->getPlayShowing();
        if (!empty($playShowingRe)) {
            \Util\LogUtil::log($log_file, "赛事尚未全部结束，不能开奖");
            exit;
        }
        /*2.根据周次查询t_user_account表中用户身上积分排名信息*/
        $limit = 180;
        $filed = 'score';
        $userAccountBiz = new \Biz\UserAccount();
        $userAccountList = $userAccountBiz->getUserAccountList($filed,$limit);
        if(empty($userAccountList)){
            \Util\LogUtil::log($log_file, "用户积分不存在，不能开奖");
            exit;
        }
        $productBiz = new \Biz\Product();
        $temp  = array();
        $productList = $productBiz->getProductListByType(2);//type = 2 表示H5互动小游戏中级和高级奖池商品
        //用户排名积分奖品数据拼接并插入数据库
        $userRankBiz = new \Biz\UserRank();
        foreach ($userAccountList as $k=>$v){
            /*根据user_id 和type，week_type判断是否已经有奖品记录*/
            $re = $userRankBiz->getUserRankByWhere($v['uid'],2);
            if($re){//数据表中已有该用户奖品记录，跳出本次循环执行下个操作
                continue;
            }
            $tmp = array();
            $tmp['user_id'] = $v['uid'];
            $tmp['nickname'] = $v['nickname'];
            $tmp['user_sort'] = $k+1;
            $tmp['user_score'] = $v[$filed];
            $tmp['week_type'] = 0;
            $tmp['type'] = 2;
            if ($tmp['user_sort'] == 1) {//1名获得俄罗斯双人7日游套餐，
                $tmp['product_id'] = $productList[0]['id'];
            } elseif ($tmp['user_sort'] >=2 && $tmp['user_sort'] <= 4) {//2~4名获得苹果X，
                $tmp['product_id'] = $productList[1]['id'];
            } elseif ($tmp['user_sort'] >= 5 && $tmp['user_sort'] <= 15) {// 5~15名获得拍立得，
                $tmp['product_id'] = $productList[2]['id'];
            } elseif ($tmp['user_sort'] >= 16 && $tmp['user_sort'] <= 30) {// 16~30名获得小米电饭煲，
                $tmp['product_id'] = $productList[3]['id'];
            } elseif ($tmp['user_sort'] >= 31 && $tmp['user_sort'] <= 50) {//31~50名获得YSL小羊皮口红，
                $tmp['product_id'] = $productList[4]['id'];
            } elseif ($tmp['user_sort'] >= 51 && $tmp['user_sort'] <= 150) {//51~150名获得运动手环，
                $tmp['product_id'] = $productList[6]['id'];
            } elseif ($tmp['user_sort'] >= 151 && $tmp['user_sort'] <= 180) {//150~180名获得官方纪念版足球。
                $tmp['product_id'] = $productList[5]['id'];
            }
            $re = $userRankBiz->insert($tmp);
            if(!$re){
                \Util\LogUtil::log($log_file, "总排行积分奖品录入失败，数据为:".json_encode($tmp,true));
                continue;
            }
        }
    }
    $transaction->commit();
} catch (Exception $e) {
    $transaction->rollBack();
    \Util\LogUtil::log($log_file, $e->getMessage());
    exit;
}